<?php
/**
 * Created by PhpStorm.
 * User: andy
 * Date: 2019/8/29
 * Time: 下午6:42
 */
namespace app\common\model;

use think\facade\Config;
use think\Model;
use think\facade\Cache;

class WebConfigModel extends Model
{
    const STATUS_SHOW=1; //显示
    const STATUS_HIDE=0; //不显示
    const STATUS_DEL=-1; //删除

    protected $autoWriteTimestamp = true;
    protected $deleteTime = 'delete_time';

        /**
     * 所有分组
     * @param int $pageSum
     * @return \think\Paginator
     * @throws \think\exception\DbException
     */
    public function getALL($where=[],$pageSum=0)
    {
        if($pageSum<=0){
            $pageSum=getSystem("pageSUM")?getSystem("pageSUM"):10;
        }

        $list=self::where($where)->order(['list_order' => 'asc', 'create_time' => 'asc'])->paginate($pageSum);

        return $list;
    }

    /**
     * 增加分组
     * @param $data
     * @return bool
     */
    public function adds($data){
        //检测当前Key名是否重复
        if(!self::chackAction($data)){
            return['status'=>0,'msg'=>'键名已存在！'];
        }

        if(empty($data['id'])){
            $status = self::save($data);

            if($status){
                return['status'=>1,'msg'=>'增加成功！'];
            }else{
                return['status'=>0,'msg'=>'增加失败！'];
            }
        }else{
            $status = self::save($data,['id'=>$data["id"]]);

            if($status){
                return['status'=>1,'msg'=>'修改成功！'];
            }else{
                return['status'=>0,'msg'=>'修改失败！'];
            }
        }

    }

    /**
     * 删除
     * @param $id
     * @return array
     */
    public function del($id){
        $au=self::get($id);
        $au->status=self::STATUS_DEL;
        $au->delete_time=time();

        $status=$au->save();
        if($status){
            return ['status'=>1,'msg'=>'删除成功！'];
        }else{
            return ['status'=>0,'msg'=>'删除失败！'];
        }

    }

    /**
     * 检测当前是否重复
     * @param $data
     * @return bool
     */
    private static function chackAction($data){
        if(empty($data['id'])){
            $data['id']=0;
        }

        $count=self::where('name',$data['name'])
            ->where('id',"<>",$data['id'])
            ->count();

        if($count>0){
            return false;
        }else{
            return true;
        }
    }

    /**
     * 读取所有系统配置
     * @param int $update 更新：false 不更新，true 更新
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public static function getPublicConfig($update=false){

        if (!Cache::get('system') || $update) {
            $list=self::field("name,value")->where('status',self::STATUS_SHOW)->select();

            $arr=[];
            foreach ($list as $vo){
                $arr[$vo['name']]=$vo['value'];
            }

            Cache::set('system',$arr);
        }
        return Cache::get('system');
    }

}